using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class CompInfo
{
    public string path;
    public string name;
    public string type;
}



public class CompomentParser
{
    public CompomentParser()
    {
        Validator.Instance.Init();
        InitMapType();
    }

    Dictionary<string,MapType> mapType = new Dictionary<string, MapType>();

    void InitMapType()
    {
        mapType.Clear();
        string cfg_path = EditorPathMgr.Instance.MapTypeCfgPath;
        // file not exist
        if(!File.Exists(cfg_path))
        {
            // create file
            var file = ScriptableObject.CreateInstance<TypeMapConfig>();
            AssetDatabase.CreateAsset(file,cfg_path);
        }
        // // read the map type from the config file
        var config = AssetDatabase.LoadAssetAtPath<TypeMapConfig>(cfg_path);
        foreach(var map in config.mapType)
        {
            mapType.Add(map.smallType,map);
        }

    }

    MapType GetTrueType(string type)
    {
        if(mapType.ContainsKey(type))
        {
            return mapType[type];
        }
        return null;
    }
    string OP_Flags = "OP"; //操作标识
    //获取Prefab的组件信息
    public List<CompInfo> GetPrefabCompInfo(GameObject prefab)
    {
        //清空pathlist
        var compList = new List<CompInfo>();
        var filters = new List<string>();

        //获取gameobject所有子节点
        Transform[] children = prefab.GetComponentsInChildren<Transform>(true);
        foreach(Transform child in children)
        {
            bool is_prefab = PrefabUtility.IsAnyPrefabInstanceRoot(child.gameObject);
            if(is_prefab)
            {
                string path = GetGameObjectPath(child.gameObject);
                path = path.Substring(path.IndexOf('/') + 1);
                filters.Add(path + "/");
            }
            //分割字符串
            string[] paths = child.name.Split('_');
            if(paths.Length > 2)
            {
                bool is_op = paths[0] == OP_Flags;
                //添加到pathlist
                if(is_op)
                {
                    string path = GetGameObjectPath(child.gameObject);
                    path = path.Substring(path.IndexOf('/') + 1);
                    bool is_filter = false;
                    if(filters.Count > 0)
                    {
                        foreach(string filter in filters)
                        {
                            if(path.Contains(filter))
                            {
                                is_filter = true;
                                break;
                            }
                        }
                    }
                    if(is_filter == false)
                    {
                        CompInfo info = new CompInfo();
                        var typeMap = GetTrueType(paths[1]);
                        var type = typeMap == null ? paths[1] : typeMap.type;
                        var nameSpace = typeMap == null ? "" : typeMap.nameSpace;

                        var validate = Validator.Instance.ValidateType(type,nameSpace);
                        if(validate == false)
                        {
                            Debug.LogError("组件类型错误：" + type);
                            return null;
                        }
                        info.path = path;
                        info.type = type;
                        info.name = paths[2] + "_" + paths[1].ToLower();
                        compList.Add(info);
                    }
                }
            }
        }

        return compList;
    }

    //递推获取gameobject tree path
    string GetGameObjectPath(GameObject go)
    {
        string path = go.name;
        Transform parent = go.transform.parent;
        while(parent != null)
        {
            path = parent.name + "/" + path;
            parent = parent.parent;
        }
        return path;
    }

    // [MenuItem("Tools/CompomentParser")]
    // static void CompomentParserMenu()
    // {
    //     string cfg_path = "Assets/Scripts/Editor/Config/TypeMapConfig.asset";
    //     // file not exist
    //     if(!File.Exists(cfg_path))
    //     {
    //         // create file
    //         var file = ScriptableObject.CreateInstance<TypeMapConfig>();
    //         AssetDatabase.CreateAsset(file,cfg_path);
    //     }
    // }
}